package ps.train.image;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import ps.train.image.Flickr;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class ViewEachImage extends Activity {

	private ImageView v;
	private String url="";
	private ProgressDialog pd;
	private DownloadImage downTask;
	private String link_comment = "http://api.flickr.com/services/rest/?method=flickr.photos.comments.getList&api_key=";
	private String api_key = Flickr.api;
	private String format_photoid ;
	private String format = Flickr.format;
	private String callback = Flickr.noJsoncall;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_each_image);
		
		v = (ImageView)findViewById(R.id.each_image);
		
		downTask = new DownloadImage(this);
		if(Flickr.nextImage==0)
		{
			downTask.execute(this.setUrl());
		}
		else if(Flickr.nextImage==1)
		{
			downTask.execute(this.nextUrl());
		}
		else if(Flickr.nextImage==2)
		{
			downTask.execute(this.preUrl());
		}
	}
	
	public String getPhotoId()
	{
		format_photoid = Flickr.photoid;
		return format_photoid;
	}
	
	public String setUrl()
	{
		url = Flickr.url;
		return url;
	}
	
	public String nextUrl()
	{			
		if(Flickr.pos==Flickr.allUrl.length-1)
		{
			Flickr.pos = 0;
			url = Flickr.allUrl[Flickr.pos];
		}
		else
		{
			Flickr.pos+=1;
			url = Flickr.allUrl[Flickr.pos];
		}
		
		return url;		
	}
	
	public String preUrl()
	{
		if(Flickr.pos==0)
		{
			Flickr.pos = Flickr.allUrl.length -1;
			url =  Flickr.allUrl[Flickr.pos];
		}
		else
		{
			Flickr.pos-=1;
			url =  Flickr.allUrl[Flickr.pos];
		}
		
		return	url;					
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		int action = event.getAction();
		int startX = (int)event.getX() ;
		int startY = (int)event.getY();
		int endX ;
		int endY ;
		int moveX = startX;

		switch(action)
		{
			case MotionEvent.ACTION_DOWN:
				startX = (int)event.getX();
				startY = (int)event.getY();
				break;
				
			case MotionEvent.ACTION_MOVE:
				moveX = (int)event.getX();
				break;
			case MotionEvent.ACTION_UP:
				endX = (int)event.getX();
				if(moveX >= v.getWidth()-5)
				{
					Flickr.nextImage = 1;
					Intent i = new Intent(getApplicationContext(), ViewEachImage.class);
					startActivity(i);
					finish();
				}
				else if(moveX <= 0)
				{
					Flickr.nextImage = 2;
					Intent i = new Intent(getApplicationContext(), ViewEachImage.class);
					startActivity(i);
					finish();
				}
				break;
			
		}
		
		return true;
	}
	

	public ProgressDialog showProDialog()
	{
		ProgressDialog p = ProgressDialog.show(this, "Loading", ".........");
		p.show();
		return p;
	}
	
	public class DownloadImage extends AsyncTask<String, Integer, Bitmap>
	{

		private Context mcon;
		
		public DownloadImage(Context con)
		{
			this.mcon = con;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {
			v.setImageBitmap(result);
			pd.dismiss();
		}

		@Override
		protected void onPreExecute() {
			pd = showProDialog();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		@Override
		protected Bitmap doInBackground(String... urls) {
			HttpClient client = new DefaultHttpClient();			
    	    HttpGet getRequest ;
    	    Bitmap image = null ;
		
		    	    try {
		    	    	getRequest = new HttpGet(urls[0]);
		    	        HttpResponse response = client.execute(getRequest);		    	       
		    			
		    	        HttpEntity entity = response.getEntity();
		    	        if (entity != null)
		    	        {
		    	        	InputStream inputStream = entity.getContent();
		    	           	
		    	        	 BitmapFactory.Options opt = new BitmapFactory.Options();		    	       
		    	        	 opt.inSampleSize = 1; // original size		    	        	
		    	        	 if (image == null )
		    	        	   try {
		    	        	     // just a sample, not only just decode file
		    	        		   image  = BitmapFactory.decodeStream(inputStream, null, opt);
		    	        	   } catch (OutOfMemoryError e) {
		    	        	     // Here is just a sample, we can use many another option to decrease bitmap size 
		    	        	     opt.inSampleSize++;
		    	        	   }

		    	                	Log.d("doInBackground","download image :");
		    	                	return image;
		    	        }
		    	    } catch (Exception e)
		    	    {
		    	        // Could provide a more explicit error message for IOException or IllegalStateException
		    	       e.printStackTrace();   	       
		    	    }
		    	    
				return null;
		
    	}
	
		
	}

}
